package question

import (
	"data/config"
	"data/view/common"
	"fmt"
	"public/mylog"
	"public/mysqldb"
	"strconv"
	"time"
)

//解析excel
func readFromExcel(data [][]string, username, q_type string) (b bool, rows_quest []string) {
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	if len(data) > 1 {
		for i := 1; i < len(data); i++ {
			var quest Question_tbl
			quest_no := common.CreateUnique(strconv.Itoa(i) + common.RECRUIT_TOPIC + q_type) //TODO
			quest.Topic = common.RECRUIT_TOPIC
			quest.Bundle = common.QuestionTypeMap[q_type]
			quest.Quest_no = quest_no
			quest.Operator = username
			quest.Pid = data[i][0]                                              //类别
			quest.Title = data[i][1]                                            //标题
			quest.Content = data[i][2]                                          //内容
			if q_type == common.SCHOICE_TYPE || q_type == common.MCHOICE_TYPE { //单选多选
				quest.Option = getOptionToString(data[i][3], data[i][4], data[i][5], data[i][6],
					data[i][7], data[i][8]) //选项
				quest.Answer = data[i][9]                                 //答案
				quest.Attach = data[i][10]                                //附加
				quest.Full_score, _ = strconv.ParseFloat(data[i][11], 64) //满分值
				quest.Limit_time, _ = strconv.Atoi(data[i][12])           //答题限时
				quest.Operator = username
				if data[i][14] == "" {
					data[i][14] = "普通"
				}
				quest.Difficulty = data[i][14] //难度
				quest.Analysis = data[i][13]   //试题解析
			} else if q_type == common.CHECK_TYPE { //判断
				quest.Option = getOptionToString(data[i][3], data[i][4]) //选项
				quest.Answer = data[i][5]                                //答案
				quest.Attach = data[i][6]                                //附加
				quest.Full_score, _ = strconv.ParseFloat(data[i][7], 64) //满分值
				quest.Limit_time, _ = strconv.Atoi(data[i][8])           //答题限时
				quest.Operator = username
				if data[i][10] == "" {
					data[i][10] = "普通"
				}
				quest.Difficulty = data[i][10] //难度
				quest.Analysis = data[i][9]    //试题解析
			} else if q_type == common.FILL_TYPE || q_type == common.BRIEF_TYPE { //填空简答
				quest.Answer = data[i][3]                                //答案
				quest.Attach = data[i][4]                                //附加
				quest.Full_score, _ = strconv.ParseFloat(data[i][5], 64) //满分值
				quest.Limit_time, _ = strconv.Atoi(data[i][6])           //答题限时
				quest.Operator = username
				if data[i][8] == "" {
					data[i][8] = "普通"
				}
				quest.Difficulty = data[i][8] //难度
				quest.Analysis = data[i][7]   //试题解析
			} else {
				return
			}
			err := orm.Create(&quest).Error
			if err != nil {
				mylog.Error(err)
				return
			}
			rows_quest = append(rows_quest, quest_no)
		}
	}
	b = true
	return
}

func getOptionToString(options ...string) string {
	var r string = ""
	if len(options) > 0 {
		for k, v := range options {
			if v != "" {
				if k > 0 {
					r += ","
				}
				r += v
			}
		}
	}
	return r
}

//创建试卷
func CreatePaperTmp(name, subject, operator string, questions []string) bool {
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	var paper Examination_tmp
	paper.Topic = common.RECRUIT_TOPIC
	paper.Name = name
	paper.Bundle = subject
	paper.Pid = common.CreateUnique(common.RECRUIT_TOPIC + "paper")
	paper.Questions = fmt.Sprintf("%s", questions[0])
	for i := 1; i < len(questions); i++ {
		paper.Questions += common.SPLIT + fmt.Sprintf("%s", questions[i])
	}
	paper.Operator = operator
	paper.Create_time = time.Now()
	err := orm.Table("examination_tbl").Create(&paper).Error
	if err != nil {
		mylog.Error(err)
		return false
	}
	return true
}
